package com.usian.springbootmysql.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.springbootmysql.dao.LoginDao;
import com.usian.springbootmysql.dao.UserDao;
import com.usian.springbootmysql.pojo.ResuitVo;
import com.usian.springbootmysql.pojo.TbUser;
import com.usian.springbootmysql.service.LoginService;
import com.usian.springbootmysql.utils.AliCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginDao loginDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate stringReditsTemplate;

    @Override
    public ResuitVo sendConde(String phone) throws Exception {
        Boolean isPhone = Validator.isMobile(phone);
        if (isPhone) {
            int count = loginDao.getUserCountByPhone(phone);
            if (count > 0) {
                AliCodeUtils aliCodeUtils = new AliCodeUtils();
                aliCodeUtils.senLoginCode(phone, "123456");
                stringReditsTemplate.opsForValue().set(phone, "123456");
                stringReditsTemplate.expire(phone, 1, TimeUnit.MINUTES);
                return new ResuitVo(200, "短信发送成功", null);
            } else {
                return new ResuitVo(303, "手机号未进行注册", null);
            }
        } else {
            return new ResuitVo(303, "手机号格式错误", null);
        }


    }

    @Override
    public ResuitVo userLogin(String phone, String code) {
        boolean isHavephone = StrUtil.isEmpty(phone);
        boolean isHaveCode = StringUtils.isEmpty(code);
        if (!isHavephone) {
            if (!isHaveCode) {
//                int count = loginDao.getUserCountByPhone(phone);
                LambdaQueryWrapper<TbUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(TbUser::getPhone,phone);
                TbUser tbUser=userDao.selectOne(lambdaQueryWrapper);
                if (tbUser!=null) {
                    String redisCode = stringReditsTemplate.opsForValue().get(phone);
                    if (!StrUtil.isEmpty(redisCode)) {
                        if (redisCode.equals(code)) {
                            return new ResuitVo(200, "source", tbUser);

                        } else {
                            return new ResuitVo(303, "验证码错误", null);

                        }
                    } else {
                        return new ResuitVo(303, "验证码过期", null);

                    }

                } else {
                    return new ResuitVo(303, "用户不存在", null);

                }
            } else {
                return new ResuitVo(303, "验证码必填", null);

            }
        } else {
            return new ResuitVo(303, "手机号未填", null);

        }



    }

}
